package org.testis.communicationmodule.services;

import org.testis.communication.client.AtofHttpClient;
import org.testis.communication.data.CommunicationModuleDataObject;
import org.testis.logging.AtofLogLevel;
import org.testis.logging.LogSenderService;
import org.testis.services.AtofModuleAction;
import org.testis.services.AtofModuleService;

/**
 * A class to define an http client action for
 * Communication module
 * @author nurhat
 */
public class HttpClientAction extends AtofModuleAction {

    /**
     * Function checks whether a given host is available
     * by using AtofHttpClient
     * @param request
     * @return
     */
    @Override
    public boolean operateRequest(AtofModuleService request) {
        CommunicationModuleDataObject dataObj =
                (CommunicationModuleDataObject) request.getDataObj();
        LogSenderService.putMessageOnQueue(
                "HttpClientAction",
                AtofLogLevel.INFO,
                "Starting to execute http client action with data:" + dataObj.toString() + "....",
                dataObj.getMessageId());

        boolean result = false;
        String userName = dataObj.getUserName(),
                passWord = dataObj.getPassword();
        if (userName != null && passWord != null) {
            if (!userName.equals("") && !passWord.equals("")) {
                result =
                        checkWithAuthentication(dataObj.getHost(),
                        userName,
                        passWord,
                        dataObj.getStringToSendAndVerify(), dataObj.getMessageId());
            } else {
                result = checkWithoutAuthentication(dataObj.getHost(),
                        dataObj.getStringToSendAndVerify(), dataObj.getMessageId());
            }
        } else {
            result =
                    checkWithoutAuthentication(dataObj.getHost(),
                    dataObj.getStringToSendAndVerify(), dataObj.getMessageId());
        }
        AtofLogLevel level = result ? AtofLogLevel.INFO : AtofLogLevel.ERROR;

        LogSenderService.putMessageOnQueue(
                "HttpClientAction",
                level,
                "HttpClientAction executed " + (result ? "" : "un")
                + "successfully...",
                dataObj.getMessageId());


        return result;
    }

    private boolean checkWithAuthentication(String host,
            String user,
            String password,
            String verifyStr, String messageId) {
        AtofHttpClient client = new AtofHttpClient();
        boolean result = false;
        if (verifyStr == null) {
            result = client.checkStatus(host,
                    user,
                    password);

        } else if (verifyStr.trim().equals("")) {
            result = client.checkStatus(host,
                    user,
                    password);
        } else {
            result = client.checkStatus(host,
                    user,
                    password,
                    verifyStr);
        }
        LogSenderService.putMessageOnQueue(
                "HttpClientAction",
                AtofLogLevel.INFO,
                client.getStatusCode() + "\n" + client.getOutput(),
                messageId);

        return result;
    }

    private boolean checkWithoutAuthentication(String host,
            String verifyStr, String messageId) {
        AtofHttpClient client = new AtofHttpClient();
        boolean result = false;
        if (verifyStr == null) {
            result = client.checkStatus(host);
        } else if (verifyStr.trim().equals("")) {
            result = client.checkStatus(host);
        } else {
            result = client.checkStatus(host,
                    verifyStr);
        }
        LogSenderService.putMessageOnQueue(
                "HttpClientAction",
                AtofLogLevel.INFO,
                client.getStatusCode() + "\n" + client.getOutput(),
                messageId);
        return result;
    }
}
